package com.szbber.medical.ui.fragment.setting

import android.graphics.Color
import android.os.Bundle
import android.view.View
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.szbber.commen.base.BaseVMFragment
import com.szbber.commen.bean.CareLevelListBean
import com.szbber.commen.bean.NurseLevelListBean
import com.szbber.commen.dialog.DialogUtils
import com.szbber.medical.R
import com.szbber.medical.databinding.*
import com.szbber.medical.viewmodel.fragment.NurseLabelSettingViewModel
import com.szbber.medical.viewmodel.fragment.NurseLevelSettingViewModel
import kotlinx.android.synthetic.main.fragment_nurselevel_setting.*


class NurseLabelSettingFragment :
    BaseVMFragment<FragmentNurselableSettingBinding, NurseLabelSettingViewModel>(),
    View.OnClickListener {


    companion object {
        fun getInstance(): NurseLabelSettingFragment {
            return NurseLabelSettingFragment()
        }
    }

    override fun getLayoutResId(): Int = R.layout.fragment_nurselable_setting

    override fun initVM(): NurseLabelSettingViewModel =
        ViewModelProvider(this, ViewModelProvider.NewInstanceFactory()).get(
            NurseLabelSettingViewModel::class.java
        )

    override fun initView(savedInstanceState: Bundle?) {
        mBinding.nurseLabelSettingModel = mViewModel

        mViewModel.getCareLevelList(activity)

        tvAddCareLabel.setOnClickListener(this)

        mViewModel.careLevelListBean.observe(this) {
            val adapter = object : BaseQuickAdapter<CareLevelListBean.Data, BaseViewHolder>(
                R.layout.item_carelevel, it.data.toMutableList()
            ) {
                override fun convert(holder: BaseViewHolder, item: CareLevelListBean.Data) {
                    val binding =
                        DataBindingUtil.bind<ViewDataBinding>(holder.itemView) as ItemCarelevelBinding
                    binding.itemcarelevel = item
                    binding.tvColor.setBackgroundColor(Color.parseColor(item.font_color))
                    binding.tvbgColor.setBackgroundColor(Color.parseColor(item.bg_color))
                    binding.tvName.setOnClickListener {
                        DialogUtils.showNurseNameDialog(activity) {
                            mViewModel.getEditCareLevel(activity, item.id.toString(), it.strValue, item.font_color, item.bg_color)
                        }
                    }
                    binding.tvColor.setOnClickListener {
                        DialogUtils.showNurseColorDialog(activity) {
                            mViewModel.getEditCareLevel(activity, item.id.toString(), item.label_name, it.strValue, item.bg_color)
                        }
                    }
                    binding.tvbgColor.setOnClickListener {
                        DialogUtils.showNurseColorDialog(activity) {
                            mViewModel.getEditCareLevel(activity, item.id.toString(), item.label_name, item.font_color, it.strValue)
                        }
                    }
                    binding.tvDelete.setOnClickListener {
                        mViewModel.deleteNurseLevelLabel(context, item.id.toString())
                    }
                    binding.executePendingBindings()
                }
            }
            val manager = LinearLayoutManager(activity)
            mRecyclerView.adapter = adapter
            mRecyclerView.layoutManager = manager
        }

    }

    override fun onClick(p0: View?) {
        when(p0?.id){
            R.id.tvAddCareLabel->{
              DialogUtils.showAddCareLabel(activity){
                  mViewModel.addNurseLevelLabel(activity, it.addCareLabelBean)
              }
            }
        }
    }


}